from excel_write import WriteExcel
from redmine_use import RedmineUse
import os

from use_mysql import UseMysql

db_config = {'host': '192.168.1.161',
                 'port': 3306,
                 'user': 'root',
                 'password': 'root',
                 'database': 'stats_report',
                 'charset': 'utf8'}

def read_text(filepath):
    lines = []
    with open(filepath,'r',encoding='UTF-8') as f:
        for line in f.readlines():
            line = line.strip('\n')
            lines.append(line)
    return lines

def deal_temp(list):
    '''处理分割逗号'''
    returnlist = []
    for ele in list:
        temp_list = ele.split(',')
        returnlist.append(temp_list)
    return returnlist

redmine = RedmineUse(204,841)
write = WriteExcel()
#cur_path = os.getcwd()
cur_path = os.path.dirname(os.path.abspath(__file__))
moudulePath = os.path.join(cur_path,r'result\moudule.txt')
bugStatusPath = os.path.join(cur_path,r'result\bugstatus.txt')
personPath = os.path.join(cur_path,r'result\person.txt')
excelPath = os.path.join(cur_path,r'result\report.xls')
tempPath = os.path.join(cur_path,r'result\temp.txt')
personList = read_text(personPath)
mouduleList = read_text(moudulePath)
bugStatusList = read_text(bugStatusPath)
tempList = read_text(tempPath)
firstList = deal_temp(tempList[0:4])
secList = deal_temp(tempList[4:7])
thirList = deal_temp(tempList[7:])
line =0
write.write_sheet(firstList,line)
line = line + len(firstList)+2
write.write_sheet(secList,line)
line = line + len(secList) + 2
write.write_sheet(thirList,line)
line = line + len(thirList) + 2
todayNewList = redmine.getTodayNewBugNum()
write.write_sheet(todayNewList,line)
line = line + len(todayNewList) + 2
allBugList = redmine.getAllBugNum()
list1 = allBugList[-1]
my = UseMysql(db_config)
my.insert_data(*list1)
select_sql = "select * from bug_report"
list2 = my.select_data(select_sql)
my.close_db()
allBugList.pop(-1)
write.write_sheet(allBugList,line)
line = line +len(allBugList)
write.write_sheet(list2,line)
line = line + len(list2) + 2
bugStatusNum = redmine.getBugStatusNum(bugStatusList)
write.write_sheet(bugStatusNum,line)
line = line + len(bugStatusNum) + 2
bugInfoList = redmine.getTodayBugInfo()
write.write_sheet(bugInfoList,line)
line = line + len(bugInfoList) + 2
tuiHuaList = redmine.getTuiHuaGeneral()
write.write_sheet(tuiHuaList,line)
line = line + len(tuiHuaList) + 2
greatBug = redmine.getGreatBug()
write.write_sheet(greatBug,line)
line = line + len(greatBug) + 2
bolockBug = redmine.getBlockBug()
write.write_sheet(bolockBug,line)
line = line + len(bolockBug) + 2
persongAllBug = redmine.getPersonAllBug(personList)
write.write_sheet(persongAllBug,line)
line = line + len(persongAllBug) +2
personSolveBug = redmine.getPersonSolveBug(personList)
write.write_sheet(personSolveBug,line)
line = line + len(personSolveBug) + 2
mouduleBug = redmine.getMoudulebug(mouduleList)
write.write_sheet(mouduleBug,line)
print('脚本成功运行完成')


write.save_excle(excelPath)
